from selenium import webdriver
from selenium.webdriver.common.by import By
from time import sleep
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


# 豆瓣同城业务：登录+感兴趣***********


driver = webdriver.Chrome()
sleep(2)
driver.set_window_size(800, 1000)
sleep(2)
driver.get('https://www.douban.com/')
sleep(3)
driver.maximize_window()
sleep(2)
# 输出浏览器信息
print(f"名称；{driver.name}, 标题：{driver.title}, URL：{driver.current_url}, 尺寸：{driver.get_window_size()}")
# 豆瓣登录线*****
# 进入frame框架
f = driver.find_element(By.XPATH, '/html/body/div[2]/div/div[1]/iframe')
driver.switch_to.frame(f)
sleep(3)

# 输入手机号
sjh = driver.find_element(By.NAME, 'phone')
sjh.send_keys('15985645480')
# 输出输入框的信息
print(f"文本值：{sjh.text}, class属性：{sjh.get_attribute('class')}, 宽高：{sjh.size}, 坐标：{sjh.location}, 元素是否可用：{sjh.is_enabled()}, 元素是否显示：{sjh.is_displayed()}, 元素是否被选中：{sjh.is_selected()}")

# 强制等待30s 输入验证码
sleep(30)
# 点击豆瓣登录
driver.find_element(By.XPATH, '/html/body/div[1]/div[2]/div[1]/div[5]/a').click()
sleep(2)

# 退出frame框架
driver.switch_to.default_content()
sleep(2)

# 豆瓣同城：感兴趣*****
# 第一步：点击同城
try:
    tc = WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable(
            (By.XPATH, '/html/body/div[1]/div/div[4]/ul/li[5]/a'))
    )
    tc.click()
except Exception as e:
    print(f"异常：{e}")
    raise
sleep(2)
# 第三步：窗口切换
driver.switch_to.window(driver.window_handles[-1])
sleep(3)
# 第四步：选择下拉框


# 滚动到底部
driver.execute_script('window.scrollTo(0, document.body.scrollHeight)')
sleep(2)
# 点击图片
driver.find_element(By.XPATH, '/html/body/div[4]/div[1]/div/div[1]/div[6]/div[10]/div[2]/ul/li[1]/div[1]/a/img').click()
sleep(2)
# 第六步：点击感兴趣
driver.find_element(By.XPATH, '/html/body/div[4]/div[1]/div/div[1]/div[1]/div[2]/div[2]/a[1]/span').click()
sleep(10)


# 点击取消（取消感兴趣）
driver.find_element(By.XPATH, '//a[@href="https://www.douban.com/event/35190100/?interest=do_cancel&ck=tM6c"]').click()
sleep(2)
# 弹窗确定关闭
alert = driver.switch_to.alert
alert.accept()

sleep(10)




